import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

public class HttpsPost {

    /**
     * 发送请求. 
     * @param httpsUrl
     *            请求的地址 
     * @param xmlStr
     *            请求的数据 
     */
    public static void post(String httpsUrl, String xmlStr) {
        HttpsURLConnection urlCon = null;
        try {
            urlCon = (HttpsURLConnection) (new URL(httpsUrl)).openConnection();
            urlCon.setDoInput(true);
            urlCon.setDoOutput(true);
            urlCon.setRequestMethod("POST");
            urlCon.setRequestProperty("Content-Type", "application/xml");
            urlCon.setRequestProperty("Content-Length",
                    String.valueOf(xmlStr.getBytes().length));
            urlCon.setUseCaches(false);
            //设置为gbk可以解决服务器接收时读取的数据中文乱码问题  
            urlCon.getOutputStream().write(xmlStr.getBytes("gbk"));
            urlCon.getOutputStream().flush();
            urlCon.getOutputStream().close();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    urlCon.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试方法. 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String httpsUrl = "https://pdata.4ggogo.com/web-in/auth.html";

        String AppKey = "b395e66de5d9458b8b8eeedd602ce7d3";
        String AppSecret = "592cf42e02304aeea3d2ca4208fc7923";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss+08:00");//设置日期格式
        String date = df.format(new Date());
        Encrypt encrypt = new Encrypt();
        String sign = encrypt.SHA256(AppKey + date + AppSecret);
        // 传输文本
        String s = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Request><Datetime>"+date+"</Datetime><Authorization><AppKey>"+AppKey+"</AppKey><Sign>"+sign+"</Sign></Authorization></Request>";
        // 发起请求
        HttpsPost.post(httpsUrl, s);
    }
}  